# Billion.py
# 百万富翁问题  主程序
# -*- coding: utf-8 -*-
"""
Created on 2021

@author: Ximing
"""
from SimpleRSA import *
import random

public_key, private_key = make_key_pair(12)  # safe for n<100
A = random.randint(1, 9)
B = random.randint(1, 9)

def safeCmpAleB(a, b):
	    print("\n假设张三有 i={} 亿，李四有 j={} 亿".format(a, b))
	    print("\n张三生成一对RSA公私钥:")
	    print("公钥（n,e)： {}".format(public_key))
	    print("私钥（n,d)： {}\n".format(private_key))
	    x = random.randint(1000, 2000)
	    print("Step 1:李四随机选取一个大整数:{} ".format(x))
	    K = public_key.encrypt(x)
	    print("\t\t李四利用张三公开的公钥对大整数进行加密得到密文K: ".format(K))
	    print("\t\t然后李四将 c=K-j({}-{}={}) 发送给张三\n".format(K, b, K - b))
	    c = K - b
	    p = 29
	    d = []
	    for i in range(c + 1, c + 11):
	        d.append((private_key.decrypt(i) % p))
	    print("Step 2:张三用自己的私钥对 c+1至c+10 进行加密：")
	    print("\t\t{}".format(d))
	    for i in range(a, 10):
	        d[i] = d[i] + 1
	    print("\t\t对c+i+1至c+10执行+1操作，得到：")
	    print("\t\t{}".format(d))
	    print("Step 3:计算 x mod p 是否等于 d[j]. \n\t\t如果是, i>=j，即张三比李四富裕\n\t\t否则,i<j，即李四比张三富裕\n")
	    print("\t\t计算：d[j] = {}, x mod p = {}".format(d[b - 1], x % p))
	    if (x % p == d[b - 1]):
	        return print("\t\t此次结果：i>=j，张三比李四富裕")
	    else:
	        return print("\t\t此次结果：i<j，李四比张三富裕")

if __name__ == '__main__':
	    safeCmpAleB(A, B)
